package {{package}};

import {{invokerPackage}}.ApiUtils
{{#imports}}import {{import}}
{{/imports}}

{{#operations}}
class {{classname}} {
    String basePath = "{{{basePath}}}"
    String versionPath = ""
    ApiUtils apiUtils = new ApiUtils();

    {{#operation}}
    def {{operationId}} ({{#allParams}} {{{dataType}}} {{paramName}},{{/allParams}} Closure onSuccess, Closure onFailure)  {
        String resourcePath = "{{{path}}}"

        // params
        def queryParams = [:]
        def headerParams = [:]
        def bodyParams
        def contentType

        {{#allParams}}
        {{#required}}
        // verify required params are set
        if ({{paramName}} == null) {
            throw new RuntimeException("missing required params {{paramName}}")
        }
        {{/required}}
        {{/allParams}}

        {{#queryParams}}
        if ({{paramName}} != null) {
            queryParams.put("{{baseName}}", {{paramName}})
        }
        {{/queryParams}}

        {{#headerParams}}
        if ({{paramName}} != null) {
            headerParams.put("{{baseName}}", {{paramName}})
        }
        {{/headerParams}}

        {{#bodyParam}}
        {{^consumes}}
        contentType = 'application/json';
        {{/consumes}}
        {{#consumes.0}}
        contentType = '{{{mediaType}}}';
        {{/consumes.0}}
        {{/bodyParam}}
        {{#bodyParams.0}}
        {{^hasMore}}
        bodyParams = {{paramName}}
        {{/hasMore}}
        {{#hasMore}}
        bodyParams = [:]
        bodyParams.put("{{baseName}}", {{paramName}})
        {{/hasMore}}
        {{/bodyParams.0}}
        {{#bodyParams}}
        {{#secondaryParam}}
        bodyParams.put("{{baseName}}", {{paramName}})
        {{/secondaryParam}}
        {{/bodyParams}}

        {{#hasFormParams}}
        {{#consumes.0}}
        contentType = '{{{mediaType}}}';
        {{/consumes.0}}
        {{#formParams.0}}
        {{^hasMore}}
        bodyParams = {{paramName}}
        {{/hasMore}}
        {{#hasMore}}
        bodyParams = [:]
        bodyParams.put("{{baseName}}", {{paramName}})
        {{/hasMore}}
        {{/formParams.0}}
        {{#formParams}}
        {{#secondaryParam}}
        bodyParams.put("{{baseName}}", {{paramName}})
        {{/secondaryParam}}
        {{/formParams}}
        {{/hasFormParams}}

        apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
                    "{{httpMethod}}", "{{returnContainer}}",
                    {{#returnBaseType}}{{{returnBaseType}}}.class {{/returnBaseType}}{{^returnBaseType}}null {{/returnBaseType}})

    }

    {{/operation}}
}
{{/operations}}
